import React from 'react';

const keyName = 'questions';

const initFormat = source => {
    if (!source) {
        return undefined;
    }
    const wrapper = extend => item => ({
        ...item,
        ...extend
    });
    const { selfInterviewQuestions = [], othersInterviewQuestions = [] } = source;
    return [...selfInterviewQuestions, ...othersInterviewQuestions.map(wrapper({ other: true }))];
};

export default function usePrepareQuestionsForm(form, sourceData) {
    const { setFieldsValue } = form;
    const formData = React.useMemo(() => initFormat(sourceData), [sourceData]);

    React.useEffect(() => {
        setFieldsValue({ [keyName]: [...formData] });
    }, [formData]);

    const getSubmitValues = React.useCallback(values => {
        const { [`${keyName}`]: arr } = values;
        const next = arr?.map(item => {
            const { id, question } = item || {};
            if (id) {
                return { id, question };
            }
            return { question };
        });
        const ret = {
            action: 'probationInterviewPrepare',
            interviewPrepareSaveDTO: {
                interviewQuestions: next,
                arrangeId: sourceData?.probationInterviewArrangeVO?.id
            }
        };
        console.log('===============================================>usePrepareQuestionsFrom Confirm Data', ret);
        return ret;
    }, []);
    return { getSubmitValues };
}
